package com.cskaoyan.wordmemorize.converter;

import com.cskaoyan.wordmemorize.dao.entity.UserCheckinDO;
import com.cskaoyan.wordmemorize.dao.entity.UserCheckinStatisticsDO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.UserCheckinDTO;
import com.cskaoyan.wordmemorize.dto.app.UserCheckinStatisticsDTO;
import com.cskaoyan.wordmemorize.request.UserCheckinCommand;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.List;

/**
 * 用户打卡数据转换器
 */
@Mapper(componentModel = "spring")
public interface UserCheckinConverter {

    /**
     * 将打卡命令转换为DO
     */
    @Mapping(target = "isDeleted", constant = "0")
    UserCheckinDO command2DO(UserCheckinCommand command);

    /**
     * 将DO转换为DTO
     */
    @Mapping(target = "id", source = "id", defaultExpression = "java(String.valueOf(userCheckinDO.getId()))")
    @Mapping(target = "userId", source = "userId", defaultExpression = "java(String.valueOf(userCheckinDO.getUserId()))")
    UserCheckinDTO userCheckinDO2DTO(UserCheckinDO userCheckinDO);

    /**
     * 将DO列表转换为DTO列表
     */
    List<UserCheckinDTO> userCheckinDOs2DTOs(List<UserCheckinDO> userCheckinDOs);

    /**
     * 将统计DO转换为DTO
     */
    @Mapping(target = "id", source = "id", defaultExpression = "java(String.valueOf(statisticsDO.getId()))")
    @Mapping(target = "userId", source = "userId", defaultExpression = "java(String.valueOf(statisticsDO.getUserId()))")
    @Mapping(target = "totalTime", source = "totalTime", defaultExpression = "java(statisticsDO.getTotalTime() == null ? \"0\" : String.valueOf(statisticsDO.getTotalTime()))")
    UserCheckinStatisticsDTO statisticsDO2DTO(UserCheckinStatisticsDO statisticsDO);

    /**
     * 将分页数据转换为PageDTO
     */
    default PageDTO<UserCheckinDTO> userCheckinPage2PageDTO(List<UserCheckinDTO> list, long total) {
        PageDTO<UserCheckinDTO> pageDTO = new PageDTO<>();
        pageDTO.setPageList(list);
        pageDTO.setTotalCount(total);
        return pageDTO;
    }
}
